import { ActionContext } from 'vuex';
import cloneDeep from 'lodash/cloneDeep';
import { FormCard } from '@/model/types/formCard';
import { FormProperty } from '@/model/designer/controlProperty';
import { ResponseHandle } from '@/global/util/preUtils';
import {
  getOrgList,
  createOrg,
  removeOrg,
  savevOrg,
  getUserListByDept,
  dimission,
  createUser,
  saveUser,
} from '@/views/organization/api';

interface State {
  isLoading: boolean;
  includeSub: boolean;
  orgs: { [key: string]: any }[];
  users: { [key: string]: any }[];
  selectedTreeKey: string[];
  selectedRowKeys: string[];
}

const getInitData = (): State => {
  return {
    isLoading: false,
    includeSub: false,
    orgs: [],
    users: [],
    selectedTreeKey: [],
    selectedRowKeys: [],
  };
};
const state: State = getInitData();
const getters = {};
const actions = {
  async getOrgList(context: ActionContext<State, any>, params: any) {
    const res: any = await getOrgList(params);
    // ResponseHandle(res, () => {
    //   res.data && context.commit('SetOrg', res.data);
    // });
    return res;
  },
  async createOrg(context: ActionContext<State, any>, params: any) {
    const res: any = await createOrg(params);
    return res;
  },
  async removeOrg(context: ActionContext<State, any>, params: any) {
    const res: any = await removeOrg(params);
    return res;
  },
  async savevOrg(context: ActionContext<State, any>, params: any) {
    const res: any = await savevOrg(params);
    return res;
  },
  async getUserListByDept(context: ActionContext<State, any>, params: any) {
    Object.assign(params, { includeSub: context.state.includeSub });
    const res: any = await getUserListByDept(params);
    if (!params.setStore) return res;
    ResponseHandle(res, () => {
      res.data && context.commit('SetUsers', res.data);
    });
  },
  async dimission(context: ActionContext<State, any>, params: any) {
    const res: any = await dimission(params);
    return res;
  },
  async createUser(context: ActionContext<State, any>, params: any) {
    const res: any = await createUser(params);
    return res;
  },
  async saveUser(context: ActionContext<State, any>, params: any) {
    const res: any = await saveUser(params);
    return res;
  },
};
const mutations = {
  SetIsLoading(state: State, data: boolean) {
    state.isLoading = data;
  },
  SetIncludeSub(state: State, data: boolean) {
    state.includeSub = data;
  },
  // SetOrg(state: State, data: any) {
  //   state.orgs = data;
  // },
  SetUsers(state: State, data: []) {
    state.users = data;
  },
  SetSelectedRowKeys(state: State, data: []) {
    state.selectedRowKeys = data;
  },
  SetSelectedTreeKey(state: State, data: []) {
    state.selectedTreeKey = data;
  },
};
export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters,
};
